
package TreeLP;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.NodeUnselectEvent;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

@ManagedBean
@ViewScoped
public class TreeBeanLP implements Serializable {

    private TreeNode root;
    private TreeNode[] selectedNodes; 
    
    
    private List<TreeNode> selectedNodesCustomList = new ArrayList<TreeNode>(); 

    public TreeBeanLP() {
        root = new DefaultTreeNode("Root", null);
        TreeNode node0 = new DefaultTreeNode("LP 0", root);
        TreeNode node1 = new DefaultTreeNode("LP 1", root);
        TreeNode node2 = new DefaultTreeNode("LP 2", root);

        TreeNode node00 = new DefaultTreeNode("PRODUCTO 0.0", node0);
        TreeNode node01 = new DefaultTreeNode("PRODUCTO 0.1", node0);

        TreeNode node10 = new DefaultTreeNode("PRODUCTO 1.0", node1);
        TreeNode node11 = new DefaultTreeNode("PRODUCTO 1.1", node1);

        TreeNode node000 = new DefaultTreeNode("Analisis 0.0.0", node00);
        TreeNode node001 = new DefaultTreeNode("Diseño 0.0.1", node00);
        TreeNode node010 = new DefaultTreeNode("Analisis 0.1.0", node01);

        TreeNode node100 = new DefaultTreeNode("Analisis 1.0.0", node10);
    }

    public TreeNode getRoot() {
        return root;
    }

    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }

    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }

    public void displaySelectedMultiple(ActionEvent event) {
        if (selectedNodes != null && selectedNodes.length > 0) {
            StringBuilder builder = new StringBuilder();

            for (TreeNode node : selectedNodes) {
                builder.append(node.getData().toString());
                builder.append("<br />");
            }
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.toString());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }
    
   
    public void onNodeSelect( NodeSelectEvent event )
    {

        TreeNode node = event.getTreeNode();
        boolean isSelected = node.isSelected();
        
        String text = "node isSelected status = " + isSelected;
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "SELECT event", text );
            FacesContext.getCurrentInstance().addMessage(null, message);        
        
        processAllChildNodes(node, isSelected );
    }    
        
    public void onNodeUnselect( NodeUnselectEvent event )
    {
        
        TreeNode node = event.getTreeNode();
        boolean isSelected = node.isSelected();
        
        String text = "node isSelected status = " + isSelected;
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "UNSELECT event", text );
            FacesContext.getCurrentInstance().addMessage(null, message);        
                
        processAllChildNodes(node, isSelected);
    }
    
    private void processAllChildNodes( TreeNode node, boolean selectValue )
    {
        
        List<TreeNode> childNodes = node.getChildren();
        if ( childNodes == null || childNodes.isEmpty() )
        {
            return;
        }
        for ( TreeNode childNode : childNodes )
        {
            childNode.setSelected( selectValue );
            processAllChildNodes( childNode, selectValue );
        }
    }
  
    public void displaySelectedMultipleCustom(ActionEvent event) 
    {
        selectedNodesCustomList.clear();
        
        List<TreeNode> nodes = getAllSelectedVarNodes();
        if ( (nodes != null) && ! nodes.isEmpty() )
        {
            StringBuilder builder = new StringBuilder();
            for ( TreeNode node : nodes )
            {
                builder.append(node.getData().toString());
                builder.append("<br />");                
            }
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.toString());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }                
    }    
    
    private List<TreeNode> getAllSelectedVarNodes()
    {
        List<TreeNode> varNodeList = new ArrayList<TreeNode>();
    
        addAllSelectedVarChildNodesToList( root, varNodeList );
        return varNodeList;
    }
    
    private void addAllSelectedVarChildNodesToList( TreeNode node, 
            List<TreeNode> varNodeList )
    {
        
        List<TreeNode> childNodes = node.getChildren();
        if ( childNodes == null || childNodes.isEmpty() )
        {
            return;
        }
        for ( TreeNode childNode : childNodes )
        {
            if ( childNode.isSelected() )
            {
                varNodeList.add(childNode);
            }
            addAllSelectedVarChildNodesToList( childNode, varNodeList );
        }
    }      
}   

